'''
Created on 26/05/2011

@author: Hagut
'''

from game_agent import GameAgent
from alpha_beta_improved import AlphaBetaSearchImproved
from our_adds.utils import PriorityQueue
from time import clock

NO_LIMIT = -1

' ~~~~~~taken from AlphaBetaAgent for refernece, should be changed to our agent ~~~~~~~'
 
class SuperAgent(GameAgent):
    '''
    This is an interface for a Game Playing Agent.
    '''
    def move(self, game_state):
        '''
        This is the method called by the runner of this agent.
        It includes the code that decides the next move.
        
        @param game_state: The current game state.
        @return: The GameAction that the agent will execute in its next move.
        '''
#        return self.alphaBeta.search(game_state, self.turn_time_limit)
        stime = clock()
        res =  self.alphaBeta.search(game_state, self.turn_time_limit)
        print "turn time",clock()-stime
        return res

    
    def setup(self, player, game_state, turn_time_limit, setup_time_limit):
        '''
        This is method is called once at the beginning of the game, 
        and enables the agent to prepare for things to come.
        
        @param player: Your player.
        @param game_state: The initial game state.
        @param turn_time_limit: The time that will be allocated for each turn.
        @param setup_time_limit: The time limit for setting up this agent.
        '''
        self.player = player
        self.turn_time_limit = turn_time_limit
        u = lambda state: self.utility(state)
        self.alphaBeta = AlphaBetaSearchImproved(self.player, 3, u, self)

    def utility(self, state):
        winner = state.getWinner()
        if winner is None:
            return 0
        elif winner == self.player:
            return 1
        else:
            return -1  
        
    def getSuccImproved(self, gameState):
        successors = PriorityQueue()
        for action, value in gameState.getSuccessors().items():
#            print "get successors action", action
#            print "get successors value", 
#            print value
            successors.append((action, value))
        #successors = gameState.getSuccessors().items()
        #f = lambda x: self.utility(x[1])
        #ordered = sorted(successors,key=f)
        return successors, None     
    
    
class DummyAgent(GameAgent):
    def move(self, game_state):
        for action in game_state.getSuccessors().keys():
            return action
    
    def setup(self, player, game_state, turn_time_limit, setup_time_limit):
        return